<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script type="text/javascript">
			function search(){
				//0.1获取地址栏查询字符串对象
				var obj=new URLSearchParams(location.search);
				//0.2通过这个对象获取uid 
				var $user_id=obj.get("user_id");
				console.log($user_id)
				//1.创建异步对象
				var xhr=new XMLHttpRequest();
				//4.创建监听，发送请求
				xhr.onreadystatechange=function(){
					if(xhr.readyState==4 && xhr.status==200){
						var r=xhr.responseText;
						console.log(r)
						if(r==0){alert("查无此人")
					}else{
						var arr=JSON.parse(r);
						receiver.value=arr[0].receiver;
						province.value=arr[0].province;
						city.value=arr[0].city;
						county.value=arr[0].county;
						address.value=arr[0].address;
						cellphone.value=arr[0].cellphone;
						postcode.value=arr[0].postcode;
						
					}
				}
			}
			//2.创建请求
				xhr.open("get",`/address/v1/search/${$user_id}`,true);
				//3.发送请求
				xhr.send();
			}
			function put(){
			var obj=new URLSearchParams(location.search);
			var $user_id=obj.get("user_id");
			var $receiver=receiver.value;
			var $province=province.value;
			var $city=city.value;
			var $county=county.value;
			var $address=address.value;
			var $cellphone=cellphone.value;
			var $postcode=postcode.value;
			//1.创建xhr异步对象
			var xhr=new XMLHttpRequest();
			//4.创建监听，接收响应
			xhr.onreadystatechange=function(){
				if(xhr.readyState==4&&xhr.status==200){
					var r=xhr.responseText;
					if(r==1){
						alert("修改成功");
						location.href="user_address_put.html"
					}
					else{alert("修改失败")}
				}
			}
			//2.创建请求，打开连接
				xhr.open("PUT",`/address/v1/user_address_revise`,true);
      var formdata=`user_id=${$user_id}&receiver=${$receiver}&province=${$province}&city=${$city}&county=${$county}&address=${$address}&cellphone=${$cellphone}&postcode=${$postcode}`;
      console.log(formdata)
      xhr.setRequestHeader("content-type","application/x-www-form-urlencoded")
      //3.发送请求
			xhr.send(formdata);
			
			}
		</script>
	</head>
	<body onload="search()">
		<input type="text" id="receiver" />接收人姓名<br>
		<input type="text" id="province" />省<br>
		<input type="text" id="city" />市<br>
		<input type="text" id="county" />县<br>
		<input type="text" id="address" />详细地址<br>
		<input type="text" id="cellphone" />手机<br>
		<input type="text" id="postcode" />邮编<br>
		<button onclick="put()">修改</button>
	</body>
</html>
